package com.gk.javaservice.controller;

import com.gk.javaservice.common.ErrorMsg;
import com.gk.javaservice.common.ServerResponse;
import com.gk.javaservice.po.AdminRoles;
import com.gk.javaservice.service.AdminRolesService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/admin/AdminRoles")
public class AdminRolesController {

    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private AdminRolesService adminRolesService;

    //添加
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/add")
    public ServerResponse<?> add(@RequestBody AdminRoles t) {
        log.info(t);

        try {
            adminRolesService.add(t);
            if (t.getId() > 0) {
                return ServerResponse.createBySuccess(t.getId());
            } else {
                return ServerResponse.createByErrorMessage("标识已经存在");
            }
        } catch (Exception e) {
            log.info(e);
            return ServerResponse.createByErrorMessage(ErrorMsg.DB_FAILED);
        }
    }

    //根据ID删除1个
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/delete")
    public ServerResponse<?> delete(@RequestBody AdminRoles t) {
        log.info("id:" + t.getId());
        adminRolesService.delete(t.getId());
        return ServerResponse.createBySuccess(t.getId());

    }

    //修改信息
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/update")
    public ServerResponse<?> update(@RequestBody AdminRoles t) {
        log.info("id:" + t.getId());
        adminRolesService.update(t);
        return ServerResponse.createBySuccess(adminRolesService.hasById(t.getId()));

    }

    //获取所有信息
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/hasAll")
    public ServerResponse<?> hasAll() {
        log.info("in to hasAll");
        return ServerResponse.createBySuccess(adminRolesService.hasAll());
    }


    //根据ID获取信息
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/hasById")
    public ServerResponse<?> hasById(@RequestBody String id) {
        log.info("id:" + id);
        return ServerResponse.createBySuccess(adminRolesService.hasById(id));

    }

    //获取所有信息
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/search")
    public ServerResponse<?> search(HttpServletRequest request) {
        log.info("in to search" + request.toString());
        return ServerResponse.createBySuccess(adminRolesService.search(request.getParameter("searchMsg")));
    }
}
